from django.shortcuts import render
from django.views.generic.base import View
from django.http import JsonResponse
from apps.operation.forms import UserFavForm,CourseCommentsForm,VideoCommentsForm
from apps.operation.models import UserFavorite,CourseComments,VideoComments
from apps.courses.models import Course
from apps.organizations.models import CourseOrg, Teacher
from django.contrib.auth.mixins import LoginRequiredMixin


# class DeleteFavView(View,LoginRequiredMixin):
#     login_url = '/login/'
#
#     def post(self, request, *args, **kwargs):
#         userfav_form = UserFavForm(request.POST)
#         if userfav_form.is_valid():
#             fav_id = request.POST.get('fav_id', '')
#             fav_type = request.POST.get('fav_type', '')
#
#             # 是否已经收藏
#             exusted_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type)
#             if exusted_records:
#                 exusted_records.delete()
#                 if fav_type == '1':
#                     course = Course.objects.get(id=fav_id)
#                     course.fav_nums -= 1
#                     course.save()
#                 elif fav_type == '2':
#                     course_org = CourseOrg.objects.get(id=fav_id)
#                     course_org.fav_nums -= 1
#                     course_org.save()
#                 elif fav_type == '3':
#                     teacher = Teacher.objects.get(id=fav_id)
#                     teacher.fav_nums -= 1
#                     teacher.save()
#
#                 return JsonResponse({
#                     'status': 'success',
#                     'msg': '删除成功'
#                 })
#             else:
#
#
#                 return JsonResponse({
#                     'status': 'fail',
#                     'msg': '未收藏'
#                 })
#         else:
#             return JsonResponse({
#                 'status': 'fail',
#                 'msg': '参数错误'
#             })
class AddVideoCommentView(View):
    def post(self, request, *args, **kwargs):
        """
        用户评论
        """
        if not request.user.is_authenticated:
            return JsonResponse({
                'status':'fail',
                'msg':'用户未登录'
            })
        VideoComments_form = VideoCommentsForm(request.POST)
        if VideoComments_form.is_valid():
            video = VideoComments_form.cleaned_data["video"]
            comments = VideoComments_form.cleaned_data["comments"]
            comment = VideoComments()
            comment.user = request.user
            comment.video = video
            comment.comments = comments
            comment.save()
            return JsonResponse({
                'status': 'success',
                'msg': '评论成功'
            })
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': '非法内容或字数超出限制'
            })


class AddCommentView(View):
    def post(self, request, *args, **kwargs):
        """
        用户评论
        """
        if not request.user.is_authenticated:
            return JsonResponse({
                'status':'fail',
                'msg':'用户未登录'
            })
        CourseComments_form = CourseCommentsForm(request.POST)
        if CourseComments_form.is_valid():
            course = CourseComments_form.cleaned_data["course"]
            comments = CourseComments_form.cleaned_data["comments"]
            comment = CourseComments()
            comment.user = request.user
            comment.course = course
            comment.comments = comments
            comment.save()
            return JsonResponse({
                'status': 'success',
                'msg': '评论成功'
            })
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': '非法内容或字数超出限制'
            })


class AddFavView(View):
    def post(self, request, *args, **kwargs):
        """
        用户收藏
        """
        if not request.user.is_authenticated:
            return JsonResponse({
                'status':'fail',
                'msg':'用户未登录'
            })
        userfav_form = UserFavForm(request.POST)
        if userfav_form.is_valid():
            fav_id = request.POST.get('fav_id', '')
            fav_type = request.POST.get('fav_type', '')

            # 是否已经收藏
            exusted_records = UserFavorite.objects.filter(user=request.user, fav_id=fav_id, fav_type=fav_type)
            if exusted_records:
                exusted_records.delete()
                if fav_type == '1':
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums -= 1
                    course.save()
                elif fav_type == '2':
                    course_org = CourseOrg.objects.get(id=fav_id)
                    course_org.fav_nums -= 1
                    course_org.save()
                elif fav_type == '3':
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums -= 1
                    teacher.save()

                return JsonResponse({
                    'status': 'success',
                    'msg': '收藏'
                })
            else:
                if fav_type == '1':
                    course = Course.objects.get(id=fav_id)
                    course.fav_nums += 1
                    course.save()
                elif fav_type == '2':
                    course_org = CourseOrg.objects.get(id=fav_id)
                    course_org.fav_nums +=1
                    course_org.save()
                elif fav_type == '3':
                    teacher = Teacher.objects.get(id=fav_id)
                    teacher.fav_nums += 1
                    teacher.save()
                user_fav = UserFavorite()
                user_fav.fav_id = fav_id
                user_fav.fav_type = fav_type
                user_fav.user = request.user
                user_fav.save()

                return JsonResponse({
                    'status': 'success',
                    'msg': '已收藏'
                })
        else:
            return JsonResponse({
                'status': 'fail',
                'msg': '参数错误'
            })